<script setup>
import { ref } from 'vue'
import { onLaunch, onShow, onLoad } from '@dcloudio/uni-app'
//引入API
import { getAddressListAPI } from '@/api/address.js'
//引入子组件

//引入props

//引入store
import { useAddressStore } from '@/store'
const addressStore = useAddressStore()
//请求数据初始化页面
const addressList = ref([])
onLoad(async () => {
  //获取地址列表
  const res = await getAddressListAPI()
  addressList.value = res.result
})

const handleEdit = (id) => {
  uni.navigateTo({
    url: `/pages/address/detail/index?id=${id}`,
  })
}

const handleAddAddress = () => {
  uni.navigateTo({
    url: '/pages/address/detail/index',
  })
}

// 定义新的处理函数
const handleAddressInfoClick = (item) => {
  addressStore.setAddress(item)
  //   console.log(addressStore.address)
  // 获取当前页面栈
  //   const pages = uni.getCurrentPages();
  //   if (pages.length > 1) {
  //     const prevPage = pages[pages.length - 2];
  //     // 这里可以根据实际条件修改判断逻辑
  //     if (prevPage.route === 'your_previous_route') {
  //       uni.navigateBack();
  //       return;
  //     }
  //   }
}
</script>

<template>
  <view>
    <view v-for="item in addressList" :key="item.id" class="address-item">
      <view class="address-info" @tap="handleAddressInfoClick(item)">
        <text class="receiver">{{ item.receiver }}</text>
        <text class="contact">{{ item.contact }}</text>
        <button class="edit-btn" @tap.stop="handleEdit(item.id)">编辑</button>
      </view>
      <text class="full-location">{{ item.fullLocation }}</text>
      <text class="address">{{ item.address }}</text>
    </view>
    <view class="add-address" @tap="handleAddAddress">新建地址</view>
  </view>
</template>

<style lang="scss">
.address-item {
  padding: 20rpx;
  border-bottom: 1rpx solid #eee;

  .address-info {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10rpx;

    .receiver {
      font-size: 32rpx;
      color: #333;
    }

    .contact {
      font-size: 28rpx;
      color: #666;
    }
  }

  .full-location {
    font-size: 28rpx;
    color: #666;
    margin-bottom: 10rpx;
  }

  .address {
    font-size: 28rpx;
    color: #666;
  }
}

.edit-btn {
  width: 120rpx;
  height: 60rpx;
  line-height: 60rpx;
  background-color: #27ba9b;
  color: #fff;
  border-radius: 10rpx;
  margin-left: 20rpx;
}

.add-address {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  line-height: 100rpx;
  text-align: center;
  background-color: #27ba9b;
  color: #fff;
  font-size: 32rpx;
}
</style>
